package org.lql.algo.codecrush.hot100.skill;

/**
 * @author: liangqinglong
 * @date: 2025-10-13 22:43
 * @description: 169. 多数元素 <a href="https://leetcode.cn/problems/majority-element/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class MajorityElement {

	public int majorityElement(int[] nums) {
		int count = 0;
		int candidate = 0;
		for (int num : nums) {
			if (count == 0) {
				candidate = num;
			}
			count += (num == candidate) ? 1 : -1;
		}
		return candidate;
	}

	public static void main(String[] args) {
		System.out.println(new MajorityElement().majorityElement(new int[]{2, 2, 1, 1, 1, 2, 2}));
		System.out.println(new MajorityElement().majorityElement(new int[]{3, 2, 3}));
	}
}
